package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.dao.LabelItemsDAO;
import com.junzhiit.nengji.shop.model.LabelItems;

@Repository
public class LabelItemsDAOImpl extends AbstractBaseDAO<LabelItems> implements LabelItemsDAO{
	@Override
	public List<LabelItems> findPageAll(LabelItems labelItems, int pageNumber, int maxPageRow){
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from label_items");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(labelItems.getValue())){
			sql.append(" and label_items=?");
			param.setString(labelItems.getValue());
		}
		
		if(StringUtils.isNotBlank(labelItems.getLabelId())){
			sql.append(" and label_id=?");
			param.setString(labelItems.getLabelId());
		}
		
		return super.queryForPagingList(sql.toString(), LabelItems.class, param, pageNumber, maxPageRow);
	}
	
	@Override
	public int getTotalCount(LabelItems labelItems){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from label_items");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(labelItems.getValue())){
			sql.append(" and label_items=?");
			param.setString(labelItems.getValue());
		}
		
		if(StringUtils.isNotBlank(labelItems.getLabelId())){
			sql.append(" and label_id=?");
			param.setString(labelItems.getLabelId());
		}
		
		return super.queryForInt(sql.toString(), param);
	}
	
	@Override
	public int getIsExist(LabelItems labelItems){
		
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from label_items");
		sql.append(" where is_delete= ? ");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(labelItems.getValue())){
			sql.append(" and value = ?");
			param.setString(labelItems.getValue());
		}
		
		if(StringUtils.isNotBlank(labelItems.getLabelId())){
			sql.append(" and label_id = ?");
			param.setString(labelItems.getLabelId());
		}
		
		return super.queryForInt(sql.toString(),param);
		
	}
	
}
